<?php
namespace app\api\controller;

use app\common\controller\Tongshangyun;
use think\Lang;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class MemberAuth extends MobileMember
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/member_auth.lang.php');
    }

    /**
     * 会员升级
     *
     * @param
     * @return
     */
    public function index()
    {
        $member_model = model('member');
        if (request()->isPost()) {
            $member_array = array();
            $member_array['member_auth_state'] = 1;
            $member_array['member_idcard'] = input('post.member_idcard');
            $member_array['member_truename'] = input('post.member_truename');
            $member_validate = validate('member');
                if (!$member_validate->scene('auth')->check($member_array)) {
                    ds_json_encode(10001,$member_validate->getError());
                }
            if(!$this->member_info['member_idcard_image1']){
              ds_json_encode(10001,lang('member_idcard_image1_require'));
            }
            if(!$this->member_info['member_idcard_image2']){
              ds_json_encode(10001,lang('member_idcard_image2_require'));
            }
            if(!$this->member_info['member_idcard_image3']){
              ds_json_encode(10001,lang('member_idcard_image3_require'));
            }
            $update = $member_model->editMember(array('member_id' => $this->member_info['member_id'],'member_auth_state'=>array('in',array(0,2))), $member_array);

            $message = $update ? lang('ds_common_save_succ') : lang('ds_common_save_fail');

            if($update){
            	if(isset($update['state']) && !$update['state']){
		            ds_json_encode(10001,$update['message']);
	            }
            	//if($member_array['member_auth_state'] == 1){

					//$tongshang=new Tongshangyun();
		            //$data=$tongshang->signContract($this->member_info['member_id'], $url);
	            //}
                ds_json_encode(10000,$message);
            }else{
                ds_json_encode(10001,$message);
            }
        }

        ds_json_encode(10000,"查询成功",['member_info'=>$this->member_info]);
    }


	/**
	 * 签名
	 * @return mixed
	 */
	public function sign_contract()
	{
		$tongshang=new Tongshangyun();
		$uid=$this->member_info['member_id'];
		$jumpUrl=input("url");
		if($this->member_info['member_auth_state'] != 3){
			ds_json_encode(10301,"未实名");
		}

		if($this->member_info['is_sign']){
			ds_json_encode(10001,"已签约");
		}

		if(!$jumpUrl){
			ds_json_encode(10002,"跳转链接不能为空");
		}

		$data=$tongshang->signContract($uid,$jumpUrl);
		$data=json_decode($data,true);
		$url="http://116.228.64.55:6900/yungateway/member/signContract.html";//测试
		//$url=" https://fintech.allinpay.com/yungateway/member/signContract.html";//正式
		$sb='';
		foreach ($data as $entry_key => $entry_value) {

			$sb .= $entry_key . '=' . urlencode($entry_value) . '&';

		}
		$site=$url.'?'.$sb;
		ds_json_encode(10000,"查询成功",['url'=>$site]);
	}
	
	/**
	 * 判断手机号
	 * @return mixed
	 */
	public function phonebind()
	{
		$member_model = model('member');

		if($this->member_info['is_phone']){
			ds_json_encode(10001,"手机号已绑定");
		}


		if (request()->isPost()) {
			$member_array = array();
			$member_array['is_phone'] = 1;
			$phone = input('post.phone');
			$code = input('post.code');
			//$member_validate = validate('member');

			if(!$phone){
				ds_json_encode(10001,"手机号不能为空");
			}
			if(!$code){
				ds_json_encode(10001,"验证码不能为空");
			}

			$tongshang=new Tongshangyun();
			$res=$tongshang->bindPhone($this->member_info['member_id'],$phone,$code);
			if(!$res['state']){
				if($res['message'] != "手机已绑定"){
					ds_json_encode(10001,$res['message']);
				}
			}
			$update = $member_model->editMember(array('member_id' => $this->member_info['member_id'],'is_phone'=>0), $member_array);

			$message = $update ? lang('ds_common_save_succ') : lang('ds_common_save_fail');

			if($update){
				ds_json_encode(10000,$message);
			}else{
				ds_json_encode(10001,$message);
			}
		}
	}


	/**
	 * 发送短信验证码
	 */
	public function phone_code()
	{
		$phone=input('phone');
		if(!$phone){
			ds_json_encode(10001,"手机号不能为空");
		}

		if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
			ds_json_encode(10001,"手机号格式不正确");
		}
		$tongshang=new Tongshangyun();
		$res=$tongshang->sendVerificationCode($this->member_info['member_id'],$phone);
		if(!$res['state']){
			ds_json_encode(10001,$res['message']);
		}else{
			ds_json_encode(10000,$res['msg']);
		}
	}

	//图片上传接口
    public function image_upload() {
        $file_name = input('param.id');
        if (!empty($_FILES[$file_name]['name'])) {

            $res=ds_upload_pic(ATTACH_IDCARD_IMAGE,$file_name);
            if(!$res['code']){
                ds_json_encode(10001,$res['msg']);
            }

            $member_array=array();
            $member_array[substr($file_name,0,20)] = $res['data']['file_name'];
            $member_model = model('member');
            if(!$member_model->editMember(array('member_id' => $this->member_info['member_id'],'member_auth_state'=>array('in',array(0,2))), $member_array)){
                ds_json_encode(10001,lang('ds_common_save_fail'));
            }
            ds_json_encode(10000,'',array('file_name'=>$res['data']['file_name'],'file_path'=>get_member_idcard_image($res['data']['file_name'])));
        }
        ds_json_encode(10001,lang('param_error'));
    }

    //删除图片接口
    public function image_drop(){
        $file_name=input('param.file_name');

        @unlink(BASE_UPLOAD_PATH . DS . ATTACH_IDCARD_IMAGE . DS . $this->member_info[$file_name]);
        $member_array=array();
        $member_array[$file_name] = '';
        $member_model = model('member');
        if(!$member_model->editMember(array('member_id' => $this->member_info['member_id'],'member_auth_state'=>array('in',array(0,2))), $member_array)){
                    ds_json_encode(10001,lang('ds_common_save_fail'));
                }
        ds_json_encode(10000,"删除成功");
    }
}